﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


using System.Windows.Input;

using System.Windows;



using FXnaAppBaseWpf;

using Microsoft.Win32;

using DotSpatial.Data;

using FXnaAppBase;

using System.IO;

using FXnaInfrastructure;

using FXnaCAD;

using FXnaGis;

using NSApplication;



namespace NSPresentation
{
    public static class AppBaseCommandsBinder
    {

        /// <summary>
        /// 将命令绑定到窗体
        /// </summary>
        /// <param name="window"></param>
        public static void BindCommandsToWindow(Window window)
        {



            //绑定NewProject命令
            CommandBinding cbNew = new CommandBinding();
            cbNew.Command = AppBaseCommands.NewProject;
            cbNew.CanExecute += new CanExecuteRoutedEventHandler(cbNew_CanExecute);
            cbNew.Executed += new ExecutedRoutedEventHandler(cbNew_Executed);
            window.CommandBindings.Add(cbNew);

            //绑定OpenProject命令
            CommandBinding cbOpen = new CommandBinding();
            cbOpen.Command = AppBaseCommands.OpenProject;
            cbOpen.CanExecute += new CanExecuteRoutedEventHandler(cbOpen_CanExecute);
            cbOpen.Executed += new ExecutedRoutedEventHandler(cbOpen_Executed);
            window.CommandBindings.Add(cbOpen);

            //绑定SaveProject命令
            CommandBinding cbSave = new CommandBinding();
            cbSave.Command = AppBaseCommands.SaveProject;
            cbSave.CanExecute += new CanExecuteRoutedEventHandler(cbSave_CanExecute);
            cbSave.Executed += new ExecutedRoutedEventHandler(cbSave_Executed);
            window.CommandBindings.Add(cbSave);

            //绑定CloseProject命令
            CommandBinding cbClose = new CommandBinding();
            cbClose.Command = AppBaseCommands.CloseProject;
            cbClose.CanExecute += new CanExecuteRoutedEventHandler(cbClose_CanExecute);
            cbClose.Executed += new ExecutedRoutedEventHandler(cbClose_Executed);
            window.CommandBindings.Add(cbClose);






            CommandBinding cbAddImage = new CommandBinding();
            cbAddImage.Command = AppBaseCommands.AddImage;
            cbAddImage.CanExecute += new CanExecuteRoutedEventHandler(cbAddImage_CanExecute);

            cbAddImage.Executed += new ExecutedRoutedEventHandler(cbAddImage_Executed);

            window.CommandBindings.Add(cbAddImage);





            CommandBinding cbRemoveImage = new CommandBinding();

            cbRemoveImage.Command = AppBaseCommands.RemoveImage;
            cbRemoveImage.CanExecute += new CanExecuteRoutedEventHandler(cbRemoveImage_CanExecute);
            cbRemoveImage.Executed += new ExecutedRoutedEventHandler(cbRemoveImage_Executed);

            window.CommandBindings.Add(cbRemoveImage);



            //AppBaseCommands.AddImage

            CommandBinding cbAddDWG = new CommandBinding();
            cbAddDWG.Command = AppBaseCommands.AddDWGFile;
            cbAddDWG.CanExecute += new CanExecuteRoutedEventHandler(cbAddDWG_CanExecute);
            cbAddDWG.Executed += new ExecutedRoutedEventHandler(cbAddDWG_Executed);

            window.CommandBindings.Add(cbAddDWG);


        }



        static void cbNew_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            ApplicationService.CurrentService.NewProject();
        }

        static void cbNew_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
            e.Handled = true;
        }

        static void cbSave_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            ApplicationService.CurrentService.SaveProject();
        }

        static void cbSave_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            if (!ProjectService.CurrentService.BNullProj)
            {
                e.CanExecute = true;
                e.Handled = true;

            }
        }
        static void cbOpen_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            ApplicationService.CurrentService.OpenProject();
        }

        static void cbOpen_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
            e.Handled = true;
        }

        static void cbClose_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            ApplicationService.CurrentService.CloseProject();
        }

        static void cbClose_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            if (!ProjectService.CurrentService.BNullProj)
            {
                e.CanExecute = true;
                e.Handled = true;
            }

        }





        static void cbAddDWG_Executed(object sender, ExecutedRoutedEventArgs e)
        {

            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "*.dwg文件(*.dwg)|*.dwg";
            ofd.CheckFileExists = true;

            if (ofd.ShowDialog() == true)
            {
                string fpath = ofd.FileName;


                FXnaGisWriter gisWriter = new FXnaGisWriter();

                CADReader reader = new CADReader();

                reader.ReadCadFile(fpath, gisWriter);


                string nameId = System.IO.Path.GetFileNameWithoutExtension(fpath);



                //读取完成之后,我们先写一个临时调试的例子

                FGroupLayer cadTest = new FGroupLayer(nameId);

                foreach (FXnaGis.FTransformedVectorLayer layer in gisWriter.DicGisLayers.Values)
                {

                    cadTest.Add(layer);


                }


                //我们尝试在主程序中，保存一份

                ProjectService.CurrentService.Project.CommonData.VectorLayers.Add(cadTest);












            }



        }

        static void cbAddDWG_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            if (!ProjectService.CurrentService.BNullProj)
            {
                e.CanExecute = true;
                e.Handled = true;

            }
        }

        static void cbRemoveImage_Executed(object sender, ExecutedRoutedEventArgs e)
        {

            Bitmap bm = e.Parameter as Bitmap;

            BitmapCollection collection = ProjectService.CurrentService.Project.CommonData.Images;

            if (collection.IsExist(bm.FileName))
            {



                collection.RemoveImage(bm);

                //资源文件夹里的图像是否要删除呢？，直接删除的话，该资源是否被占用呢？
                //默认先不删除吧


            }





        }

        static void cbRemoveImage_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
            e.Handled = true;


        }

        static void cbAddImage_Executed(object sender, ExecutedRoutedEventArgs e)
        {

            //ProjectService.CurrentService.Project.CommonData.Images




            OpenFileDialog ofd = new OpenFileDialog();
            ofd.CheckFileExists = true;

            if (ofd.ShowDialog() == true)
            {
                string fpath = ofd.FileName;

                //需要的操作可能包括，
                //复制图片资源到项目
                //包装文件引用，并添加


                string ImagesPath = ProjectService.CurrentService.GetProjPath(ProjPath.Images);


                BitmapCollection collection = ProjectService.CurrentService.Project.CommonData.Images;


                string filename = Path.GetFileName(fpath);


                if (collection.IsExist(filename))
                {
                    MessageBox.Show("相同的文件名已经存在");
                    return;

                }


                ///如果移动文件成功

                if (FileHelper.MoveFile(fpath, ImagesPath + filename))
                {


                    Extent ext = ProjectService.CurrentService.Project.CommonData.WorkBoundary.LocalExtent;


                    Bitmap mb = new Bitmap(filename, ext);


                    collection.AddImage(mb);




                }
                else
                {
                    MessageBox.Show("导入文件失败，请查找文件并确定其并未被占用");
                }





            }

        }

        static void cbAddImage_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
            e.Handled = true;

        }



    }
}
